package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IDataSetDao;
import com.yeshine.opendata.entity.DataSet;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.dto.StatisticsResult;
import com.yeshine.opendata.service.IDataSetService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by taohj on 2014/7/10.
 */
@Service
public class DataSetServiceImpl extends ServiceBase<DataSet> implements IDataSetService {


    @Autowired
    private IDataSetDao dao;

    @Override
    public Result<DataSet> create(DataSet dataSet) {

        dataSet.setCreateTime(new Date());
        dataSet.setStatus(new SysDictionary(111));

        if(dao.insert(dataSet)<=0){
            return  new Result<DataSet>(false,"新增数据集失败");
        }
        return new Result<DataSet>(true,"新增成功");
    }

    @Override
    public Result<DataSet> remove(DataSet dataSet) {
        Result<DataSet> result=new Result<DataSet>(dao.delete(dataSet)>0);
        result.setMessage(result.isSuccess()?"删除成功":"删除失败");
        return result;
    }

    @Override
    public Result<DataSet> modify(DataSet dataSet) {
        Result<DataSet> result=new Result<DataSet>(dao.update(dataSet)>0);
        result.setMessage(result.isSuccess()?"更新成功":"更新失败");
        return result;
    }

    @Override
    public Result<DataSet> getById(Integer id) {
        return new Result<DataSet>(dao.selectById(id));
    }

    @Override
    public Result<DataSet> getList(DataSet dataSet) {
        return new Result<DataSet>(dao.selectList(dataSet));
    }

    @Override
    public Result<DataSet> getByPage(PageResult<DataSet> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<DataSet>(pageResult);
    }

    @Override
    public List<StatisticsResult> statisticsByOrg(DataSet dataSet) {
        return dao.statisticsByOrg(dataSet);
    }

    @Override
    public List<StatisticsResult> statisticsByTags(DataSet dataSet) {
        return dao.statisticsByTags(dataSet);
    }

    @Override
    public List<StatisticsResult> statisticsByFormat(DataSet dataSet) {
        return dao.statisticsByFormat(dataSet);
    }

    @Override
    public Result<DataSet> joinMyOrg(DataSet dataSet) {
        DataSet ds=this.getById(dataSet.getId()).getTarget();
        dao.deleteDataSetOrg(ds);
        ds.setOrg(dataSet.getOrg());
        return new Result<DataSet>(dao.insertDataSetOrg(ds)>0);
    }

    @Override
    public Result<DataSet> removeFromMyOrg(DataSet dataSet) {
        return new Result<DataSet>(dao.deleteDataSetOrg(dataSet)>0);
    }
}
